<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/20
 * Time: 下午8:46
 */

namespace App\Http\Controllers;

use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\GroupChatMsgServices;
use App\Http\Services\MsgServices;
use App\Http\Services\UnionRankServices;

class UnionRankController extends BaseController
{
    protected $unionRankService;
    protected $msgService;
    protected $groupChatMsgService;
    public function __construct()
    {
        $this->unionRankService = new UnionRankServices();
        $this->msgService = new MsgServices();
        $this->groupChatMsgService = new GroupChatMsgServices();
    }

    /**
     * 获取公会等级相关列表
     */
    public function getRankList(){
        do {
            if (!$this->checkRequest()) {
                break;
            }

            $this->callCommon($this->unionRankService, 'getRankList', $this->requestData);

        } while (false);

        if(is_string($this->responseData)) $this->responseData = json_decode($this->responseData, true);
        Response::returnResult($this->responseData['code'], $this->responseData['data'], $this->responseData['msg']);
    }

    /**
     * 设置公会财富等级和会长特权
     */
    public function setWealth(){
        $res = $this->unionRankService->setWealth();
        if($res['code'] == 0 && !empty($res['data'])){
            //发消息
            foreach ($res['data'] as $k=>$v){
                $this->msgService->sendUnionPrivilege($k ,$v['user_id'] ,$v['privilege_name']);
            }
        }
        OutJson::outJsonNoData(Hint::SUCCESS,[]);
    }
    /**
     * 设置公会活跃等级
     */
    public function setActiveWealth(){
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $this->callCommon($this->unionRankService, 'setActiveWealth', $this->requestData);

        } while (false);

        $this->responseData();
    }

    /**
     * 资质认证
     */
    public function getQualificationInfo(){
        do {
            if (!$this->checkRequest()) {
                break;
            }

            $this->callCommon($this->unionRankService, 'getQualificationInfo', $this->requestData);

        } while (false);

        if(is_string($this->responseData)) $this->responseData = json_decode($this->responseData, true);
        Response::returnResult($this->responseData['code'], $this->responseData['data'], $this->responseData['msg']);
    }

    /**
     * 根据用户每日首次消费钻石数更新贡献值
     */
    public function updateContributionByConsume(){
        do {
            if (!$this->checkRequest(false)) {
                break;
            }

            $res = $this->callCommon($this->unionRankService, 'updateContributionByConsume', $this->requestData,false);
            if($res['code'] == 0 && !empty($res['data'])){
                $user_ids = array_keys($res['data']);
                foreach ($user_ids as $user_id){
                    $msg = '恭喜获得20公会贡献值！通过完成【每日房间单笔消费500钻】获得';
                    $this->msgService->send_sys_text_msg($user_id ,$msg);
                }
            }

        } while (false);

        $this->responseData();
    }

    /**
     * 公会礼包
     */
    public function getBagList(){
        do {
            if (!$this->checkRequest()) {
                break;
            }

            $this->callCommon($this->unionRankService, 'getBagList', $this->requestData);

        } while (false);

        $this->responseData();
    }

    /**
     * 公会礼包领取
     */
    public function bagReceive(){
        do {
            if (!$this->checkRequest()) {
                break;
            }

            $this->callCommon($this->unionRankService, 'bagReceive', $this->requestData);

        } while (false);

        $this->responseData();
    }

    public function getPresidentPrivilege(){
        do {
            if (!$this->checkRequest()) {
                break;
            }

            $this->callCommon($this->unionRankService, 'getPresidentPrivilege', $this->requestData);

        } while (false);

        if(is_string($this->responseData)) $this->responseData = json_decode($this->responseData, true);
        Response::returnResult($this->responseData['code'], $this->responseData['data'], $this->responseData['msg']);
    }

    /**
     * 每日零点定时更新公会信息到redis
     */
    public function setTodayUnionInfoToRedis(){
        do {
            if (!$this->checkRequest(false)) {
                break;
            }
            $this->callCommon($this->unionRankService, 'setTodayUnionInfoToRedis', $this->requestData);

        } while (false);

        $this->responseData();
    }
}